<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>menu</title>
		<link rel="stylesheet" href="lib/layui/css/layui.css" />

		<link rel="stylesheet" href="layui/css/layui.css" media="all">
		<link rel="stylesheet" type="text/css" href="lib/layui/css/layui.css" media="all" />
		<link rel="stylesheet" href="layui_ext/dtree/dtree.css">
		<link rel="stylesheet" href="layui_ext/dtree/font/dtreefont.css">
		<link rel="stylesheet" href="js/lay-module/dtree/dtree.css">
		<link rel="stylesheet" href="js/lay-module/dtree/font/dtreefont.css">
		<style>
			.layui-btn:not(.layui-btn-lg):not(.layui-btn-sm):not(.layui-btn-xs) {
				height: 34px;
				line-height: 34px;
				padding: 0 8px;
			}
		</style>
	</head>

	<body>
	  <span>  <a href="Modulesinfo_add.html" class="layui-btn">添加节点</a></span>
		<span>  <a href="Modulesinfo_update.html" class="layui-btn">修改节点</a></span>
		
			
			<div style="height: 400px;overflow: auto; width: 220px;" id="toolbarDiv">
			<ul id="toolbarTree1" class="dtree" data-id="0"></ul>
		</div>
		
			  <form  >
			<div class="layui-form-item">
				<label class="layui-form-label">节点名称</label>
				<div class="layui-input-inline">
					<input type="text" id="" name="" class="layui-input" />
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">权重</label>
				<div class="layui-input-inline">
					<input type="text" id="" name="" class="layui-input" />
				</div>
			</div>
			<div class="layui-form-item" style="
    margin-left: 60px;
">
				<button class="layui-btn">添加</button>
			</div>
		</form>
	 

		
		<script type="text/javascript" src="js/User.js"></script>
		<script type="text/javascript" src="lib/layui/layui.js"></script>

		<script>
			layui.extend({
				dtree: '{/}layui_ext/dtree/dtree' // {/}的意思即代表采用自有路径，即不跟随 base 路径
			}).use(['dtree', 'layer', 'jquery', 'form'], function() {
				var dtree = layui.dtree,
					layer = layui.layer,
					$ = layui.jquery,
					form = layui.form;

				// 初始化树
				var DemoTree = dtree.render({
					elem: "#toolbarTree1",
					url: globalData.server + 'dynamicMeun/getMenuAll',
					dataStyle: "layuiStyle", //使用layui风格的数据格式
					dataFormat: "list", //配置data的风格为list
					response: {
						message: "msg",
						statusCode: 0
					}, //修改response中返回数据的定义
					checkbar: true ,
					checkbarType: "self", 
					initLevel: "1",
					line: true,
					toolbar: true,
					record: true,
					dataFormat: "list",
					scroll: "#toolbarDiv",
					toolbarBtn: [
						[{
								"label": "路径",
								"name": "Permission",
								"type": "text"
							}, {
								"label": "权重",
								"name": "Weight",
								"type": "text"
							},
							{
								"value": "重置",
								"name": "reset",
								"type": "reset"
							},
							{
								"value": "保存",
								"name": "submit",
								"type": "submit",
								"defElem": "btn"
							},
						], // 这就是自定义新增中的内容
						[{
								"label": "编辑父节点",
								"name": "ParentId",
								"type": "text"
							}, {
								"label": "路径",
								"name": "Permission",
								"type": "text"
							}, {
								"label": "权重",
								"name": "Weight",
								"type": "text"
							},
							{
								"value": "保存",
								"name": "submit",
								"type": "submit",
								"defElem": "btn"
							}
						] // 自定义编辑页的内容
					],
					toolbarFun: {
						loadToolbarBefore: function(buttons, param, $div) {
							if(param.leaf) { // 如果是叶子节点
								buttons.addToolbar = ""; // 取消新增功能
							}
							return buttons; // 将按钮对象返回
						},
						addTreeNode: function(treeNode, $div) {
							$.ajax({
								type: "post",
								data: {
									"title": treeNode.addNodeName,
									"parentID": treeNode.parentId,
									"path": treeNode.Path,
									"weight": treeNode.Weight,

								},
								url: globalData.server + "statics/modinfo/addModuses",
								success: function(result) {
									//DTree1.changeTreeNodeAdd(treeNode.nodeId); // 添加成功，返回ID
									//DTree1.changeTreeNodeAdd(true); // 添加成功
									if(result.count > 0) {
										layer.msg('添加成功', {
											icon: 6
										});
										setTimeout(function() {
											window.location.reload();
										}, 1500)
									} else {
										DTree1.changeTreeNodeAdd(false); // 添加失败
										layer.alert("同一个父节点下节点名称不能相同", {
											icon: 5,
											closeBtn: 0,
											title: "添加失败"
										});
									}
									//DTree1.changeTreeNodeAdd(result.data); // 添加成功，返回一个JSON对象
									//DTree1.changeTreeNodeAdd("refresh"); // 添加成功，局部刷新树
								}
							});
						},
						editTreeNode: function(treeNode, $div) {

							$.ajax({
								type: "post",
								data: {
									"title": treeNode.editNodeName,
									"parentID": treeNode.ParentId,
									"path": treeNode.Path,
									"weight": treeNode.Weight,

									"id": treeNode.nodeId
								},
								url: globalData.server + "statics/modinfo/updateModuses",
								success: function(result) {
									console.log(result);
									if(result.count > 0) {
										DemoTree.changeTreeNodeEdit(true); // 修改成功
										layer.alert("修改成功", {
											icon: 6,
											closeBtn: 0,
											title: "修改成功"
										});
									} else {
										DemoTree.changeTreeNodeEdit(false); //修改失败
										layer.alert("同一个父节点下节点名称不能相同", {
											icon: 5,
											closeBtn: 0,
											title: "修改失败"
										});
									}
									//DTree1.changeTreeNodeEdit(result.param); // 修改成功，返回一个JSON对象
								}
							});
						},
						delTreeNode: function(treeNode, $div) {
							$.ajax({
								type: "post",
								data: {
									"id": treeNode.nodeId
								},
								url: globalData.server + "statics/modinfo/deleteModuses",
								success: function(result) {
									if(result.count > 0) {
										DTree1.changeTreeNodeDel(true); // 删除成功
										layer.alert("删除成功", {
											icon: 1,
											closeBtn: 0,
											title: "删除成功"
										});
									} else {
										DTree1.changeTreeNodeDel(false); // 删除失败
										layer.alert("删除失败", {
											icon: 2,
											closeBtn: 0,
											title: "删除失败"
										});
									}
								}
							});
						}
					}
				});

				// 绑定节点点击
				dtree.on("node('demoTree')", function(obj) {
					layer.msg(JSON.stringify(obj.param));
				});
			});
		</script>
	</body>

</html>